package leetcode;

import javax.swing.tree.TreeNode;

public class Solution8 {
    public int countNodes(TreeNode root) {
        if(root == null){
            return 0;
        }
        TreeNode left = root.left;
        TreeNode right = root.right;
        int leftDepth = 0;
        int rightDepth = 0;
        //循环遍历
        while(left != null){
            left = left.left;
            leftDepth++;
        }
        while(right != null){
            right = right.right;
            rightDepth++;
        }
        //判断左右两侧深度相等的情况
        if(leftDepth == rightDepth){
            return ((2 << leftDepth) - 1);
        }

        int leftLength = countNodes(root.left);
        int rightLength = countNodes(root.right);


        return leftLength + rightLength + 1;
    }
}
